<?php

namespace App\Repositories;

use App\Models\Category;
use InfyOm\Generator\Common\BaseRepository;

/**
 * Class CategoryRepository
 * @package App\Repositories
 * @version February 15, 2018, 9:49 am UTC
 *
 * @method Category findWithoutFail($id, $columns = ['*'])
 * @method Category find($id, $columns = ['*'])
 * @method Category first($columns = ['*'])
*/
class CategoryRepository extends BaseRepository
{
    /**
     * @var array
     */
    protected $fieldSearchable = [
        'title',
        'pic',
        'sort'
    ];

    /**
     * Configure the Model
     **/
    public function model()
    {
        return Category::class;
    }

    public function getLevelData($level=2,$is_show=0, $cate_ids=null)
    {
        $model = Category::where('level',$level)->orderBy('pid','asc')->orderBy('sort','asc');
        if($is_show)
            $model = $model->where('is_show',1);
        $categories = $model->get();
        if($level==1){
            foreach ($categories as &$category) {
                $category->child = Category::where('pid',$category->id)->get();
            }
        }else{
            foreach ($categories as &$category) {
                $category->parent = Category::where('id',$category->pid)->first();
            }
        }
        return $categories;
    }
}
